<!--
  * 计费规则
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-modal :title="props.options.title" :width="1400" :open="openForm" @cancel="onClose" :maskClosable="false" :destroyOnClose="true" :footer="false">
    <div>
      <a-form class="smart-query-form">
        <a-row class="smart-query-form-row">
          <a-col :span="4">
            <a-form-item label="分账入账户" class="smart-query-form-item">
              <a-input v-model:value="queryForm.no" placeholder="输入分账入账户" />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="是否退款" class="smart-query-form-item">
              <a-select v-model:value="queryForm.type" allowClear :options="SERVICE_TYPE" placeholder="请选择是否退款" />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="退款状态" class="smart-query-form-item">
              <a-select v-model:value="queryForm.settleType" allowClear :options="SETTLE_TYPE" placeholder="请选择退款状态" />
            </a-form-item>
          </a-col>
          <a-col :span="4">
            <a-form-item label="分账状态" class="smart-query-form-item">
              <a-select v-model:value="queryForm.settleType" allowClear :options="SETTLE_TYPE" placeholder="请选择分账状态" />
            </a-form-item>
          </a-col>
          <!-- 
      <a-form-item label="状态" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.status" enum-name="STATUS_ENUM" placeholder="状态" />
      </a-form-item> 
      -->
          <a-col :span="4">
            <a-form-item class="smart-query-form-item">
              <a-button type="primary" @click="onSearch">
                <template #icon>
                  <SearchOutlined />
                </template>
                查询
              </a-button>
              <a-button @click="resetQuery" class="smart-margin-left10">
                <template #icon>
                  <ReloadOutlined />
                </template>
                重置
              </a-button>
            </a-form-item></a-col
          >
        </a-row>
      </a-form>
      <!---------- 查询表单form end ----------->

      <a-card size="small" :bordered="false" :hoverable="true">
        <!---------- 表格操作行 begin ----------->
        <a-row class="smart-table-btn-block">
          <div class="smart-table-title">数据列表</div>
          <!-- <div class="smart-table-operate-block">
          </div> -->
          <div class="smart-table-setting-block">
            <!-- <TableOperator v-model="columns" :tableId="null" :refresh="getList" /> -->
          </div>
        </a-row>
        <!---------- 表格操作行 end ----------->

        <!---------- 表格begin ----------->
        <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
          <template #bodyCell="{ record, column }">
            <template v-if="column.dataIndex === 'action'">
              <div class="smart-table-operate">
                <a-button @click="view(record)" type="link">退款明细</a-button>
              </div>
            </template>
          </template>
        </a-table>

        <!---------- 表格 end ----------->

        <!-- <div class="smart-query-table-page">
          <a-pagination
            showSizeChanger
            showQuickJumper
            show-less-items
            :page_sizeOptions="PAGE_SIZE_OPTIONS"
            :defaultpage_size="queryForm.pageSize"
            v-model:current="queryForm.pageNo"
            v-model:pageSize="queryForm.pageSize"
            :total="total"
            @change="getList"
            @showSizeChange="getList"
            :show-total="(total) => `共${total}条`"
          />
        </div> -->
      </a-card>
    </div>
    <!-- <template #footer>
      <a-space>
        <a-button @click="onUpdataClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template> -->
  </a-modal>

  <a-modal title="退款明细" :width="1200" :open="openList" @cancel="onClose01" :maskClosable="false" :destroyOnClose="true" :footer="false">
    <div>
      <a-card size="small" :bordered="false" :hoverable="true">
        <!---------- 表格begin ----------->
        <a-table size="small" :dataSource="tableData01" :columns="columns01" rowKey="id" bordered :loading="tableLoading01" :pagination="false">
          <!-- <template #bodyCell="{ record, column }">
            <template v-if="column.dataIndex === 'action'">
              <div class="smart-table-operate">
                <a-button @click="view(record)" type="link" v-privilege="'trade:update'">退款明细</a-button>
              </div>
            </template>
          </template> -->
        </a-table>

        <!---------- 表格 end ----------->
      </a-card>
    </div>
  </a-modal>
</template>
<script setup name="TradeFlow">
  import { reactive, ref, onMounted, getCurrentInstance, computed } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { chargePage, chargeDelete, chargeUpdateStatus } from '/@/api/business/b-operation/channel/charge-api.js'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import dayjs from 'dayjs'
  // import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { selectDictLabel, formatToMoney } from '/@/utils/common'
  import {
    BILLING_TYPE, //计费类型
    CHARGE_USER_TYPE, //	计费对象
    CHARGE_TYPE, //计费模式
    SETTLE_TYPE, //'结算模式'
    SERVICE_TYPE, //'服务类型'
  } from '/@/constants/dict'
  import { isEmpty } from 'lodash'

  const props = defineProps({
    open: {
      type: Boolean,
      default: false,
    },
    options: {
      type: Object,
      default: () => {
        return {
          title: '自动分账明细列表',
        }
      },
    },
    queryData: {
      type: Object,
      default: () => {
        return {}
      },
    },
  })

  const { proxy } = getCurrentInstance()

  const emit = defineEmits(['update:open', 'ok', 'cancel'])
  const openForm = computed({ get: () => props.open, set: (val) => emit('update:open', val) })
  // ---------------------------- 表格列 ----------------------------
  const columns = ref([
    // {
    //   title: '序号',
    //   dataIndex: 'index',
    //   customRender: ({ index }) => {
    //     return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
    //   },
    // },
    {
      title: '交易订单号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '交易时间',
      dataIndex: 'fTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      width: 180,
      ellipsis: true,
    },
    {
      title: '商户名称',
      dataIndex: 'title',
      ellipsis: true,
    },
    {
      title: '业务类型',
      dataIndex: 'type',
      customRender: ({ text }) => {
        return selectDictLabel(BILLING_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '支付通道',
      dataIndex: 'title',
      ellipsis: true,
    },
    {
      title: '结算模式',
      dataIndex: 'settleType',
      customRender: ({ text }) => {
        return selectDictLabel(SETTLE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '转账类型/支付方式',
      dataIndex: 'chargeType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '交易金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '是否退款',
      dataIndex: 'chargeUserType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_USER_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '分账金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '服务费',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '实收金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '结算金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '订单状态',
      dataIndex: 'status',
      ellipsis: true,
    },
    {
      title: '分账状态',
      dataIndex: 'status',
      ellipsis: true,
    },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 80,
    },
  ])
  // ---------------------------- 查询数据表单和方法 ----------------------------

  const queryFormState = {
    type: undefined, //计费类型
    settleType: undefined, //结算类型
    chargeType: undefined, //计费模式
    chargeUserType: undefined, //计费用户类型
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    getList()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    getList()
  }

  // 查询数据
  async function getList() {
    tableLoading.value = true
    try {
      let queryResult = await chargePage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }
  function onClose() {
    // openForm.value = false
    emit('cancel', openForm.value)
  }

  onMounted(getList)

  // ---------------------------- 查明细 ----------------------------
  const openList = ref(false)
  const columns01 = ref([
    // {
    //   title: '序号',
    //   dataIndex: 'index',
    //   customRender: ({ index }) => {
    //     return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
    //   },
    // },
    {
      title: '分账订单号',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '分账时间',
      dataIndex: 'cTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '分账出账户',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '分账入账户',
      dataIndex: 'id',
      ellipsis: true,
    },
    {
      title: '入账账户类型',
      dataIndex: 'type',
      customRender: ({ text }) => {
        return selectDictLabel(BILLING_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '入账方姓名',
      dataIndex: 'title',
      ellipsis: true,
    },
    {
      title: '分账金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '是否退款',
      dataIndex: 'settleType',
      customRender: ({ text }) => {
        return selectDictLabel(SETTLE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'chargeType',
      customRender: (column) => formatToMoney(column.text / 100),
      ellipsis: true,
    },
    {
      title: '分账状态',
      dataIndex: 'chargeType',
      customRender: ({ text }) => {
        return selectDictLabel(CHARGE_TYPE, text)
      },
      ellipsis: true,
    },
    {
      title: '分账失败原因',
      dataIndex: 'chargeUserType',
      ellipsis: true,
    },
  ])
  const tableData01 = ref([])
  const tableLoading01 = ref(false)
  function onClose01() {
    openList.value = false
  }
  function view(record) {
    openList.value = true
    // queryForm.id = record.id
    // getList()
  }
</script>
